<template>
  <el-dialog
    center
    :title="{ add: '新增', edit: '编辑' }[model]"
    width="600px"
    :visible.sync="isShow"
  >
    <el-form label-width="120px" ref="form" :model="form" :rules="rules">
      <el-form-item label="企业编号" prop="eid">
        <el-input v-model="form.eid" />
      </el-form-item>
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="企业简称" prop="short_name">
        <el-input v-model="form.short_name" />
      </el-form-item>
      <el-form-item label="企业简介" prop="intro">
        <el-input v-model="form.intro" />
      </el-form-item>
      <el-form-item label="企业备注" prop="remark">
        <el-input v-model="form.remark" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="btn">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { enterpriseAdd, enterpriseEdit } from '@/api/enterprise.js'
export default {
  // props: ['isShow'],
  data () {
    return {
      model: 'add',
      isShow: false,
      form: {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      rules: {
        eid: [{ required: true, message: '必填', trigger: 'change' }],
        name: [{ required: true, message: '必填', trigger: 'change' }],
        short_name: [{ required: true, message: '必填', trigger: 'change' }],
        intro: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          if (this.model === 'edit') {
            /* 获取表单数据并且向服务器请求 */
            enterpriseEdit(this.form).then(() => {
              this.isShow = false
              this.$message.success('编辑成功')
              this.$emit('getData')
            })
          } else {
            enterpriseAdd(this.form).then(() => {
              // console.log(this.form)
              this.isShow = false
              this.$message.success('验证通过')
              this.$emit('getData')
              this.form = ''
            })
          }
        } else {
          this.$message.error('验证失败')
        }
      })
    },
    closeEvent () {
      this.isShow = false
    }
  }
}
</script>

<style lang="less">
.btn {
  text-align: center;
}
.el-dialog__header {
  background-color: #409eff;
}
.el-dialog__title {
  color: #fff;
}
</style>
